package com.blb.zycommunity.config;

import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysOperLog;
import com.blb.zycommunity.service.impl.SysOperLogServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class LogAspect {

    @Autowired
    private SysOperLogServiceImpl sysOperLogService;

    //private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Around("@annotation(autoLog)")
    public Object doAround(ProceedingJoinPoint joinPoint, AutoLog autoLog){
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        //操作日志对象
        SysOperLog sysOperLog = new SysOperLog();

        //注解内容(操作类型)
        int value = autoLog.value();
        sysOperLog.setBusinessType(value);

        //操作人
        //读取用户
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //System.out.println(user.getUsername());
        //String username = user.getUsername();
        sysOperLog.setOperName(name);

        //操作人ip
        String ipAddress = request.getRemoteAddr();
        if (ipAddress.equals("0:0:0:0:0:0:0:1")){
            ipAddress = "127.0.0.1";
        }
        sysOperLog.setOperIp(ipAddress);

        //模块标题
        String title = autoLog.title();
        sysOperLog.setTitle(title);

        //操作地点
        sysOperLog.setOperLocation("内网ip");

        //请求方式
        String requestMethod = request.getMethod();
        sysOperLog.setRequestMethod(requestMethod);

        //请求路径
        String servletPath = request.getServletPath();
        sysOperLog.setOperUrl(servletPath);

        //方法路径
        String methodPath = joinPoint.getSignature().getDeclaringTypeName();
        sysOperLog.setMethod(methodPath);
        ////方法名称
        //String methodName = joinPoint.getSignature().getName();

        //请求参数
        StringBuilder str = new StringBuilder();
        Object[] args = joinPoint.getArgs();
        for(Object arg : args){
            //参数类型
            Class<?> aClass = arg.getClass();
            str.append(aClass+":"+arg+",");
        }
        sysOperLog.setOperParam(str.toString());

        //操作时间
        LocalDateTime nowTime = LocalDateTime.now();
        sysOperLog.setOperTime(nowTime);

        //执行具体的接口
        ResponseResult responseResult = null;
        //是否执行成功
        int successStatus = 1;
        try {
            responseResult = (ResponseResult) joinPoint.proceed();
            //执行成功
            successStatus = 0;
            sysOperLog.setStatus(successStatus);
            //获取返回值
            sysOperLog.setJsonResult(responseResult.toString());
            //添加进数据库
            sysOperLogService.save(sysOperLog);

        } catch (Throwable throwable) {
            //获取报错信息
            sysOperLog.setErrorMsg(throwable.getMessage());
            //添加进数据库
            sysOperLogService.save(sysOperLog);
            throwable.printStackTrace();
        }

        return responseResult;
    }


}
